﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Mvc;
using Sharp.DataTransferObject;
using Sharp.Infrastructure;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
// ReSharper disable Mvc.ViewNotResolved

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    [AutomaticTemplate]
    public class RoleController : SharpController
    {
        public ActionResult Index()
        {
            var vm = new RoleIndexViewModel();

            return View(vm);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="param"></param>
        /// <param name="userId">用户Id</param>
        /// <remarks></remarks>
        /// <returns></returns>
        
        public ActionResult GetPaged(DataTableParameter param, int? userId)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var page = proxy.Channel.GetRolePaged(param.ToPagingFeature());

                if (userId.HasValue)
                {
                    var user = proxy.Channel.GetUserByKey(userId.Value).Data;
                    page.Data.ForEach(x =>
                    {
                        x.Selected = user.Roles.Any(r => r.Id == x.Id);
                    });
                }
                return this.Store(page);
            }
        }


        [Permission("新增", 1, "001003001")]
        public async Task<ActionResult> Create()
        {
            var model = new RoleViewModel();
            model.OrgIdDataSource = await OrgController.RootNode();

            return View(model);
        }


        [Permission("编辑", 2, "001003002")]
        public async Task<ActionResult> Edit(int id)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var model = proxy.Channel.GetRoleByKey(id).To<RoleViewModel>();
                model.OrgIdDataSource = await OrgController.RootNode();
                return View(model);
            }
        }


        
        public ActionResult Save(RoleDTO vm)
        {
            if (vm == null)
            {
                return this.JsonNet(new Result() { Err = "提交数据出错。" });
            }
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var res = proxy.Channel.SaveRole(vm);

                return this.JsonNet(res);
            }
        }

        
        [Permission("删除", 3, "001003003")]
        public ActionResult Delete(int[] ids, FormCollection collection)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var res = proxy.Channel.DeleteRoles(ids);
                return this.JsonNet(res);
            }
        }


        public ActionResult UserDialog(int? roleid)
        {
            return View("UserDialog", roleid);
        }

        /// <summary>
        /// 关联用户
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleIds"></param>
        /// <returns></returns>
        [Permission("分配角色", 3, "001003004")]
        public ActionResult Relation(int userId, int[] roleIds)
        {
            using (var proxy = new WebApiClientServiceProxy<IUserService>())
            {
                var res = proxy.Channel.RelationUser(userId, roleIds);

                return this.JsonNet(res);
            }
        }


        public ActionResult LoadWindowForUser(int userId)
        {
            ViewBag.Title = "选取用户";
            ViewBag.userId = userId;
            ViewBag.DataUrl = Url.Action("GetPaged", new { userId = userId });
            ViewBag.PostUrl = Url.Action("Relation");
            ViewBag.Save = "new Role().relation(event,'" + userId + "')";
            return View("../User/WindowForUser");
        }
    }
}